
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
react-intersection-observer-hook
Advanced tools
  . So, you can use it in your TypeScript projects too.
Live demo is here.
You can check the browser compatibility from here.
If you want to support the browsers those are not supporting it natively, you can use this polyfill.
npm install react-intersection-observer-hook
Here is a simple code to use the hook. Just pass the ref callback
to the component that you want to track its visibility. You can find a more complete code in the example
folder.
import React, { useEffect } from 'react';
import { useIntersectionObserver } from 'react-intersection-observer-hook';
// ...
function Example() {
// `useIntersectionObserver` returns a tuple.
// We need to give this `ref` callback to the node we want to observe.
// The second item, `entry` is the response of the initially created `IntersectionObserver` instance.
const [ref, { entry }] = useIntersectionObserver();
const isVisible = entry && entry.isIntersecting;
useEffect(() => {
console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`);
}, [isVisible]);
return <SomeComponentToTrack ref={ref} />;
}
if you have a scrollable container, you can set a root
like this:
import React, { useEffect } from 'react';
import { useIntersectionObserver } from 'react-intersection-observer-hook';
// ...
function Example() {
const [ref, { entry, rootRef }] = useIntersectionObserver();
const isVisible = entry && entry.isIntersecting;
useEffect(() => {
console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`);
}, [isVisible]);
return (
<ScrollableContainer
// We use `rootRef` callback to set our root node.
ref={rootRef}
>
<SomeComponentToTrack ref={ref} />
</ScrollableContainer>
);
}
If you just want to track visibility, you can use useTrackVisibility
hook.
It has the same API as useIntersectionObserver
hook. It just returns additional fields in its second tuple item.
import React, { useEffect } from 'react';
import { useTrackVisibility } from 'react-intersection-observer-hook';
// ...
function Example() {
// `useTrackVisibility` also returns a tuple like `useIntersectionObserver`.
// First item is the same `ref` callback to set the node to observe.
// Second item is an object that we can use to decide if a node is visible.
// `entry`: Same object which is returned by `useIntersectionObserver`.
// `rootRef`: Same ref callback which is returned by `useIntersectionObserver`.
// `isVisible`: Becomes true/false based on the response of `IntersectionObserver`.
// `wasEverVisible`: When our observed node becomes visible once, this flag becomes `true` and stays like that.
const [
ref,
{ entry, rootRef, isVisible, wasEverVisible },
] = useTrackVisibility();
useEffect(() => {
console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`);
}, [isVisible]);
return <SomeComponentToTrack ref={ref} />;
}
Both useIntersectionObserver
and useTrackVisibility
gets the same arguments. And those are;
For more info, you can check here and here.
Thanks goes to these wonderful people (emoji key):
KimSeonghyeon 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
React hook to use IntersectionObserver declaratively.
The npm package react-intersection-observer-hook receives a total of 98,335 weekly downloads. As such, react-intersection-observer-hook popularity was classified as popular.
We found that react-intersection-observer-hook demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.